import 'package:flutter/material.dart';
import 'package:honeypotflutter/publicBussine/bussine_colors.dart';
import 'package:honeypotflutter/publicBussine/bussine_font.dart';

class MGItems extends StatelessWidget {
  /// 左侧图片
  final String? leadIcon;

  /// 左侧标题
  final String? leadTitle;

  /// 左标题样式
  final TextStyle? leadStyle;

  /// 右侧标题
  final String? trailingTitle;

  /// 右标题样式
  final TextStyle? trailingStyle;

  final Widget? trailingWidget;

  /// 右侧图片
  final String? trailingIcon;

  /// 排序方式
  final MainAxisAlignment mainAxisAlignment;

  /// 排序方式
  final CrossAxisAlignment crossAxisAlignment;

  /// 偏移【默认左右是0，上下16】
  final EdgeInsetsGeometry? padding;

  /// 外偏移【默认0】
  final EdgeInsetsGeometry? margin;

  /// 点击事件
  final Function()? action;

  /// 背景色
  final Color color;

  /// 是否显示底部分割线
  final bool showBottomSplit;

  const MGItems({
    super.key,
    this.leadIcon,
    this.leadTitle,
    this.leadStyle,
    this.trailingTitle,
    this.trailingStyle,
    this.trailingIcon,
    this.mainAxisAlignment = MainAxisAlignment.end,
    this.crossAxisAlignment = CrossAxisAlignment.center,
    this.padding,
    this.trailingWidget,
    this.margin,
    this.action,
    this.showBottomSplit = false,
    this.color = Colors.white,
  });

  @override
  Widget build(BuildContext context) {
    return Container(
        padding: padding ?? EdgeInsets.only(top: 16, bottom: 16),
        margin: margin,
        decoration: BoxDecoration(
          color: color,
          border: Border(
            bottom: BorderSide(
              color: MGColors.splitColor,
              width: showBottomSplit ? 1 : 0,
            ),
          ),
          // border: BorderSide
        ),
        child: InkWell(
          child: Flex(
            direction: Axis.horizontal,
            children: [
              Container(
                /// 左侧Icon
                child: (leadIcon != null)
                    ? Padding(
                        padding: EdgeInsets.only(right: 10),
                        child: Image.asset(
                          leadIcon!,
                          width: 22,
                        ),
                      )
                    : null,
              ),
              Container(
                /// 标题
                child: (leadTitle != null)
                    ? Padding(
                        padding: EdgeInsets.only(right: 10),
                        child: Text(leadTitle!,
                            style: leadStyle ??
                                TextStyle(
                                  fontSize: MGFont.textFont2,
                                  color: MGColors.text1Color,
                                )),
                      )
                    : null,
              ),
              Expanded(
                child: Row(
                  children: [
                    (trailingWidget == null) ? Container() : trailingWidget!,
                    (trailingTitle != null)
                        ? Expanded(
                            child: Text(
                            trailingTitle ?? "",
                            textAlign: (mainAxisAlignment == MainAxisAlignment.start) ? TextAlign.start : TextAlign.end,
                            style: trailingStyle ?? TextStyle(color: MGColors.text3Color, fontSize: MGFont.textFont2),
                          ))
                        : Container()
                  ],
                  mainAxisAlignment: mainAxisAlignment,
                ),
              ),
              Container(
                /// Icon
                child: (trailingIcon != null)
                    ? Padding(
                        padding: EdgeInsets.only(left: 5),
                        child: Image.asset(
                          trailingIcon!,
                          width: 20,
                        ),
                      )
                    : null,
              )
            ],
          ),
          onTap: action,
        ));
  }

  /*
  * 扩展Widget
  * */
  Widget expandedCustomWidget() {
    if (trailingWidget != null && trailingTitle != null) {
      return Expanded(
          child: Row(
        children: [
          Text(
            trailingTitle!,
            textAlign: (mainAxisAlignment == MainAxisAlignment.start) ? TextAlign.start : TextAlign.end,
            // textDirection: (mainAxisAlignment == MainAxisAlignment.start) ? TextDirection.ltr : TextDirection.rtl,
            style: trailingStyle ?? TextStyle(fontSize: MGFont.textFont2, color: MGColors.text2Color),
          ),
          trailingWidget!
        ],
        mainAxisAlignment: mainAxisAlignment,
        crossAxisAlignment: crossAxisAlignment,
      ));
    }

    if (trailingWidget != null) {
      if (trailingWidget is StatelessWidget) {
        return Expanded(
            child: Row(
          children: [trailingWidget!],
          mainAxisAlignment: mainAxisAlignment,
        ));
      }
      if (trailingWidget is StatefulWidget) {
        return Expanded(child: trailingWidget!);
      }
      return Expanded(
        child: trailingWidget!,
      );
    }

    if (trailingTitle != null) {
      return Expanded(
          child: Text(
        trailingTitle!,
        textAlign: (mainAxisAlignment == MainAxisAlignment.start) ? TextAlign.start : TextAlign.end,
        style: trailingStyle ?? TextStyle(fontSize: MGFont.textFont2, color: MGColors.text2Color),
      ));
    }
    return Expanded(child: Container());
  }
}
